package com.aube.base;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.interact.base.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.model.VideoModel;
import com.huyn.baseframework.utils.StringUtils;
import com.huyn.baseframework.utils.Utils;

/**
 * Created by hill on 16/12/19.
 */

public class VideoGrid extends LinearLayout {
    private final String TAG = "Aube/VideoItem";
    private Context mContext;

    private FrameLayout mVideoImgLy;
    private TextView mVideoTitle, mVideoDesc;
    private ImageView mVideoImg;

    public VideoGrid(Context context) {
        this(context, null);
    }
    public VideoGrid(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public VideoGrid(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    public void initView(VideoModel data) {
        setOrientation(VERTICAL);

        mVideoImgLy = new FrameLayout(mContext);

        int width = 0;
        int height = 0;
        mVideoImg = new ImageView(mContext);
        mVideoImg.setBackgroundColor(mContext.getResources().getColor(R.color.grey));
        mVideoImg.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if(StringUtils.isNotBlank(data.pic)) {
            width = mContext.getResources().getDimensionPixelOffset(R.dimen.recommendvideo_width);
            height = mContext.getResources().getDimensionPixelOffset(R.dimen.recommendvideo_height);
            ImageLoader.getInstance(getContext()).fillImage(mVideoImg, data.pic, width, height);
        }
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
        mVideoImgLy.addView(mVideoImg, params);

        mVideoDesc = new TextView(mContext);
        mVideoDesc.setText(data.videoDesc);
        mVideoDesc.setTextSize(10);
        mVideoDesc.setTextColor(getResources().getColor(R.color.white));
        mVideoDesc.setGravity(Gravity.CENTER);
        mVideoDesc.setBackgroundColor(getResources().getColor(R.color.lightGrey));
        mVideoDesc.setPadding(Utils.dip2px(mContext, 4), 0, Utils.dip2px(mContext, 4), 0);
        params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, getResources().getDimensionPixelOffset(R.dimen.textview_height_S));
        params.gravity = Gravity.BOTTOM | Gravity.RIGHT;
        mVideoImgLy.addView(mVideoDesc, params);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(mVideoImgLy, layoutParams);

        mVideoTitle = new TextView(mContext);
        mVideoTitle.setText(data.videoTitle);
        mVideoTitle.setTextColor(getResources().getColor(R.color.white_70));
        mVideoTitle.setTextSize(12);
        layoutParams = new LinearLayout.LayoutParams(getResources().getDimensionPixelOffset(R.dimen.recommendvideo_width), ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = Utils.dip2px(mContext, 5);
        addView(mVideoTitle, layoutParams);
    }

    public void setLayoutParams(int index) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if(index != 0) {
            params.leftMargin = Utils.dip2px(mContext, 15);
        }
        setLayoutParams(params);
    }
}
